package com.bootdo.reptile.inteface.imp;

import com.bootdo.china.domain.ChinaData;
import com.bootdo.china.service.ChinaDataService;
import com.bootdo.reptile.ReptileFactory;
import com.bootdo.reptile.inteface.TaskConsumer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 /**
 　　* @author 郝江波
 　　* @date 2019/9/3 19:42
 　　*/
public class Task implements TaskConsumer {
    ChinaDataService chinaDataService;

    public Task(ChinaDataService chinaDataService){
        this.chinaDataService = chinaDataService;
    }


    public static ConcurrentHashMap<String,CityModel> cityCatch = new ConcurrentHashMap();

        @Override
        public void call(String str, ReptileFactory next,String host) {
            {
                Document doc =  Jsoup.parse(str);
                Elements elements = doc.select("tr.provincetr");
                //省级
                for(Element item : elements){
                    for(Element  a :  item.getElementsByTag("a")){
                        String[] split = a.attr("href").split("\\.");
                        CityModel cityModel = new CityModel();
                        cityModel.setId(split[0]);
                        cityModel.setName(a.text());
                        cityCatch.put(split[0],cityModel);
                        ChinaData chinaData  =  chinaDataService.get(Long.parseLong(cityModel.getId()));
                        if(null == chinaData){
                            chinaData = new ChinaData();
                            chinaData.setId(Long.parseLong(cityModel.getId()));
                            chinaData.setName(cityModel.getName());
                            chinaData.setParentId(0l);
                            chinaDataService.save(chinaData);
                        }
                        host = host.substring(0,host.lastIndexOf("/"));
                        host += "/";
                         next.runTask("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/"+a.attr("href"),this);
                    }
                }

                 //市级
                elements =  doc.select("tr.citytr");
                for(Element item : elements){
                    if(item.getElementsByTag("a").size() <= 0){
                        continue;
                    }
                    Element  a =  item.getElementsByTag("a").get(1);
                    //System.out.println(a.text() + "-" + a.attr("href"));
                    String[] split = a.attr("href").split("/");
                    //前2位 升级代码  后2位是级代码
                    String  temp = split[1].split("\\.")[0];
                    String s0 = temp.substring(0, 2);
                    String s1 = temp.substring(2, 4);
                    CityModel cityModel2 = new CityModel();
                    cityModel2.setId(s1);
                    cityModel2.setName(a.text());
                    ChinaData chinaData  =  chinaDataService.get(Long.parseLong(s0+s1));
                    if(null == chinaData){
                        chinaData = new ChinaData();
                        chinaData.setId(Long.parseLong(s0+s1));
                        chinaData.setName(cityModel2.getName());
                        chinaData.setParentId(Long.parseLong(s0));
                        chinaDataService.save(chinaData);
                    }
                     host = host.substring(0,host.lastIndexOf("/"));
                     host += "/";
                     next.runTask(host+a.attr("href"),this);
                }


                //区级
                elements = doc.select("tr.countytr");
                for(Element item : elements){
                    if(item.getElementsByTag("a").size() <= 0){
                        continue;
                    }
                    Element  a =  item.getElementsByTag("a").get(1);
                        String[] split = a.attr("href").split("/");
                        String  temp = split[1].split("\\.")[0];
                        String s0 = temp.substring(0, 2);
                        String s1 = temp.substring(2, 4);
                        String s2 = temp.substring(4, 6);
                        CityModel cityModel2 = new CityModel();
                        cityModel2.setId(s2);
                        cityModel2.setName(a.text());
                        ChinaData chinaData  =  chinaDataService.get(Long.parseLong(s0+s1+s2));
                        if(null == chinaData){
                            chinaData = new ChinaData();
                            chinaData.setId(Long.parseLong(s0+s1+s2));
                            chinaData.setName(cityModel2.getName());
                            chinaData.setParentId(Long.parseLong(s0+s1));
                            chinaDataService.save(chinaData);
                        }
                        host = host.substring(0,host.lastIndexOf("/"));
                        host += "/";
                       next.runTask(host+a.attr("href"),this);

                }



                //镇级
                elements = doc.select("tr.towntr");
                for(Element item : elements){
                    if(item.getElementsByTag("a").size() <= 0){
                        continue;
                    }
                    Element  a =  item.getElementsByTag("a").get(1);
                        System.out.println(a.text() + "-" + a.attr("href"));
                        String[] split = a.attr("href").split("/");
                        String  temp = split[1].split("\\.")[0];
                        String s0 = temp.substring(0, 2);
                        String s1 = temp.substring(2, 4);
                        String s2 = temp.substring(4, 6);
                        String s3 = temp.substring(6, 9);
                        CityModel cityModel2 = new CityModel();
                        cityModel2.setId(s3);
                        cityModel2.setName(a.text());
                        ChinaData chinaData  =  chinaDataService.get(Long.parseLong(s0+s1+s2+s3));
                        if(null == chinaData){
                            chinaData = new ChinaData();
                            chinaData.setId(Long.parseLong(s0+s1+s2+s3));
                            chinaData.setName(cityModel2.getName());
                            chinaData.setParentId(Long.parseLong(s0+s1+s2));
                            chinaDataService.save(chinaData);
                        }
                        host = host.substring(0,host.lastIndexOf("/"));
                        host += "/";
                        next.runTask(host+a.attr("href"),this);

                }


                //村级别
                elements = doc.select("tr.villagetr");
                for(Element item : elements){
                        Element a =   item.getElementsByTag("td").get(0);
                        Element c =   item.getElementsByTag("td").get(2);
                        String temp = a.text();
                        String s0 = temp.substring(0, 2);
                        String s1 = temp.substring(2, 4);
                        String s2 = temp.substring(4, 6);
                        String s3 = temp.substring(6, 9);
                        String s4 = temp.substring(9, 12);
                        CityModel cityModel2 = new CityModel();
                        cityModel2.setId(s4);
                        cityModel2.setName(c.text());
                        ChinaData chinaData  =  chinaDataService.get(Long.parseLong(s0+s1+s2+s3+s4));
                        if(null == chinaData){
                            chinaData = new ChinaData();
                            chinaData.setId(Long.parseLong(s0+s1+s2+s3+s4));
                            chinaData.setName(cityModel2.getName());
                            chinaData.setParentId(Long.parseLong(s0+s1+s2+s3));
                            chinaDataService.save(chinaData);
                        }

                }
        }
    }


    /**
    * 省市区模型
    */
    public static class CityModel {
        public String id ;
        public String name;
        public Map<String,CityModel> childCty = new HashMap<String,CityModel>();

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Map<String, CityModel> getChildCty() {
            return childCty;
        }

        public void setChildCty(Map<String, CityModel> childCty) {
            this.childCty = childCty;
        }
    }
}